package com.acme.bankapp.service.email;

import java.util.Random;

import com.acme.bankapp.domain.email.Email;
import com.acme.bankapp.domain.email.Queue;
import com.acme.bankapp.domain.email.impl.EmailImpl;

public class EmailServiceTester {

	public static void main(final String[] args) {
        new Thread(new EmailServiceTesterPutter()).start();
        new Thread(new EmailServiceTesterGetter()).start();
        System.out.println("Thread main is finished");
}
}

class EmailServiceTesterPutter implements Runnable {
public void run() {
        EmailService service = EmailService.getEmailService();
        for (int i = 0; i < 20; i++) {
                if ((i+1) % 15 == 0)
                        service.close(); // close queue on 15 step
                Email email = new EmailImpl();
                email.setFrom("User_" + new Random().nextDouble());
                email.setTo("To_" + new Random().nextDouble());
                email.setSubject("Subject_" + new Random().nextDouble());
                email.setMessage("Message_" + new Random().nextDouble());
                service.sendNotificationEmail(email);
                try {
                        Thread.sleep(500);
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }
        }
        service.close();
}
}

class EmailServiceTesterGetter implements Runnable {
public void run() {
        try {
                EmailService service = EmailService.getEmailService();
                Queue queue = service.getQueue();
                Thread.sleep(6000);
                // service.close();
                for (int i = 0; i < 100; i++) {
                        queue.getEmail();
                        Thread.sleep(200);
                }
        } catch (InterruptedException e) {
                e.printStackTrace();
        }
}
}
